package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckitemService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {

    @Reference
    private CheckitemService checkitemService;
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;

    @PostMapping("/add")
    public Result addCheckitem(@RequestBody CheckItem checkItem) {
        try {
            checkitemService.addCheckitem(checkItem);

            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 带条件分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")//CHECKITEM_QUERY关键字权限 才可以查询检查项数据
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkitemService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    public Result deleteCheckitem(Integer id) {
        try {
            checkitemService.deleteCheckitem(id);

            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 据id查检查项
     * @param id
     * @return
     */
    @GetMapping("findCheckitemByid")
    public Result findCheckitemByid(Integer id){
        try {
            CheckItem checkItem = checkitemService.findCheckitemByid(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("updateCheckitem")
    public Result updateCheckitem(@RequestBody CheckItem checkItem) {
        try {
            checkitemService.updateCheckitem(checkItem);

            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckItem> checkItemList = checkitemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
